<template>
	<view>
		<view style="margin: 20px;">
			<button type="primary" plain="true" @click="getRecord()">getRecord</button>
		</view>
		<view style="margin: 20px;">
			<button type="primary" plain="true" @click="startRecord(false)">开始录屏1 - 静音</button>
		</view>
		
		<view style="margin: 20px;">
			<button type="primary" plain="true" @click="startRecord(true)">开始录屏1 - 有声音</button>
		</view>
		
		<view style="margin: 20px;">
			<button type="primary" plain="true" @click="getRecordInfo()">获取录屏信息</button>
		</view>
		
		
		<view style="margin: 20px;">
			<button type="primary" plain="true" @click="stopRecord()">结束录屏</button>
		</view>
		
		录屏信息: 
		<view>
			<textarea v-model="recordInfo">
		</view>
		
	</view>
</template>

<script>
	const RecordModule = uni.requireNativePlugin('RecordModule')
	export default {
		data() {
			return {
				record: null,
				recordInfo:''
			}
		},
		methods: {
			getRecord(){
					// if(!this.record) {
					// 	this.record = uni.requireNativePlugin('RecordModule')
					// }
					this.record = RecordModule
					
					uni.showToast({title: "获取录制组件:"+JSON.stringify(this.record), icon: "none"})
					
					return this.record
			},
			startRecord(isAudio){
				let option = {
					ratio: 3, // 视频清晰度, 建议取值: 1,2,3,4,5,6; 越大越清晰, 默认3 (值越大内存要求越高, 太高容易内存溢出闪退)
					frame: 30, // 视频帧率, 建议取值: 15,20,25,30,60; 越大越流畅, 默认30
					isAudio: isAudio, // 是否录制声音, 默认false
					// fileName:
				}
				this.getRecord().startRecord(option, res=>{
					if(res.code == 0){
						// {code:0, msg:"开始录屏"}
						uni.showToast({title: "已开始录制", icon: "none"})
					}else{
						// {code:501, msg:"您的系统不支持录屏功能"}
						// {code:502, msg:"您已拒绝录屏权限"}
						// {code:503, msg:"您已拒绝录屏操作"}
						// {code:504, msg:"当前窗口禁止录屏"}
						uni.showToast({title: "录制失败:"+JSON.stringify(res), icon: "none"})
					}
				})
			},
			getRecordInfo(){
				const resp = this.getRecord().getRecordInfo()
				this.recordInfo = JSON.stringify(resp)
			},
			// 停止录制
			stopRecord(){
				let res = this.getRecord().stopRecord()
				if(res.code == 0){
					// {code:0, msg:"录制成功", path:"本地视频路径"}
					console.log("本地视频路径:" + res.path) // 可继续调uni.uploadFile()上传至服务器
					uni.showToast({title: "录制的视频已保存到相册", icon: "none"})
				}else{
					// {code:501, msg:"您还未开始录屏"}
					uni.showToast({title: "操作失败:"+JSON.stringify(res), icon: "none"})
				}
			},
		}
	}
</script>

<style>

</style>
